using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Module.Multimedia.Library;
using NCS.Module.Multimedia.Library.Gallery;
using NCS.Library.Utilities;
using NCS.Library;

namespace NCS.Module.Multimedia.Web.Gallery
{
    public partial class Gallery : GalleryWebControl
    {
        int i = 0;
        public string lblPage = "";
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            int totalRecord;
            if (Request.QueryString["page"] != null)
            {
                txtHiddenValue.Text = ConvertUtility.ToString((ConvertUtility.ToInt32(Request.QueryString["page"]) - 1));
            }
            else
            {
                txtHiddenValue.Text = "0";
            }
            int pagesize = 20;
            DataTable dtGalleryAlbum;
            int _albumid = ConvertUtility.ToInt32(Request.QueryString["albumid"]);
            if (_albumid > 0)
            {
                dtGalleryAlbum = Gallery_AlbumController.GetAllAlbumByParentID(_albumid, ConvertUtility.ToInt32(AppEnv.PortalId()), pagesize, ConvertUtility.ToInt32(txtHiddenValue.Text), out totalRecord);
            }
            else
            {
                dtGalleryAlbum = Gallery_AlbumController.GetAllAlbumByParentID(0, ConvertUtility.ToInt32(AppEnv.PortalId()), pagesize, ConvertUtility.ToInt32(txtHiddenValue.Text), out totalRecord);
            }
            rptGallery.DataSource = dtGalleryAlbum;
            rptGallery.ItemDataBound += new RepeaterItemEventHandler(rptGallery_ItemDataBound);
            rptGallery.DataBind();

            #region paging

            double pagenum = totalRecord % pagesize;
            if (pagenum == 0)
            {
                for (int i = 1; i < totalRecord / pagesize + 1; i++)
                {
                    if (ConvertUtility.ToInt32(txtHiddenValue.Text) == 0)
                    {
                        if (i == 1)
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                        }
                        else
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                        }
                    }
                    else if (i == ConvertUtility.ToInt32(txtHiddenValue.Text) + 1)
                    {
                        lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                    }
                    else
                    {
                        lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                    }
                }
            }
            else if (totalRecord % pagesize > 0)
            {
                for (int i = 1; i < totalRecord / pagesize + 2; i++)
                {
                    if (ConvertUtility.ToInt32(txtHiddenValue.Text) == 0)
                    {
                        if (i == 1)
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                        }
                        else
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                        }
                    }
                    else if (i == ConvertUtility.ToInt32(txtHiddenValue.Text) + 1)
                    {
                        lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                    }
                    else
                    {
                        lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                    }
                }
            }
            #endregion
        }
        void rptGallery_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex < 0) return;
            DataRowView currow = (DataRowView)e.Item.DataItem;
            Image imgPic = (Image)e.Item.FindControl("imgPic");
            HyperLink hplalbum = (HyperLink)e.Item.FindControl("hplalbum");

            if (Gallery_AlbumController.GetAllAlbumByParentID(ConvertUtility.ToInt32(currow["AlbumID"])).Rows.Count > 0)
            {
                hplalbum.NavigateUrl = BuildUrlByAlbum(ConvertUtility.ToInt32(currow["AlbumID"]));
                if (ConvertUtility.ToString(currow["AlbumAvatar"]) != string.Empty)
                {
                    imgPic.ImageUrl = MultimediaUtility.GetGalleryAvatar(currow["AlbumAvatar"].ToString());
                    imgPic.Attributes.Add("onclick", "window.location='" + BuildUrlByAlbum(albumid) + "'");
                    imgPic.Attributes.Add("style", "cursor:hand");
                }
                else
                    imgPic.Visible = false;
            }
            else
            {
                hplalbum.NavigateUrl = BuildUrlByPicInAlbum(ConvertUtility.ToInt32(currow["AlbumID"]));
                if (ConvertUtility.ToString(currow["AlbumAvatar"]) != string.Empty)
                {
                    imgPic.ImageUrl = MultimediaUtility.GetGalleryAvatar(currow["AlbumAvatar"].ToString());
                    imgPic.Attributes.Add("onclick", "window.location='" + BuildUrlByPicInAlbum(albumid) + "'");
                    imgPic.Attributes.Add("style", "cursor:hand");
                }
                else
                    imgPic.Visible = false;
            }
            i++;
            
            Label lblClose = (Label)e.Item.FindControl("lblClose");
            if((i % 4) == 0)
            {
                
                lblClose.Text = "</ul></div><div class=\"child c1 first\"><ul class=\"linkedimglinklist18 cf w75\">";
            }
            else
            {
                 lblClose.Text ="";
            }
        }
    }
}